package com.itheima.test.designDocument;


import com.itheima.enums.NoDrawingsApproveEnum;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

//建筑工程设计文件用章
@RunWith(SpringRunner.class)
@SpringBootTest
public class DesignDocument {

    public Map getVaribales() {

        Map<String, Object> variables = new HashMap<>();
        //技术文件用印洽商变更类
      //  variables.put(NoDrawingsApproveEnum.USESEALTYPE.getValue(),NoDrawingsApproveEnum.TECHNOLOGYREGARDINGTHECHANGE.getValue());
        //技术文件用印质量验收类  y有问题
       //variables.put(NoDrawingsApproveEnum.USESEALTYPE.getValue(),NoDrawingsApproveEnum.TECHNOLOGYQUALITYACCEPTANCE.getValue());
         // 技术文件用章 往来联系类
     // variables.put(NoDrawingsApproveEnum.USESEALTYPE.getValue(),NoDrawingsApproveEnum.TECHNOLOGYCORRESPONDENCECLASS.getValue());

        variables.put(NoDrawingsApproveEnum.PROJECTMANAGER.getValue(),Arrays.asList("projectManager1","projectManager2"));  //项目经理
        variables.put(NoDrawingsApproveEnum.MANAGINGDIRECTOR.getValue(),Arrays.asList("managingDirector1","managingDirector2"));

       variables.put(NoDrawingsApproveEnum.DESIGNHOST.getValue(),Arrays.asList("designHost1","designHost2"));  //设计主持人
        variables.put(NoDrawingsApproveEnum.DEPARTMENTHEAD.getValue(),Arrays.asList("departmentHead1","departmentHead2"));  //部门负责人
        variables.put(NoDrawingsApproveEnum.ENERGYSAVINGSEALAPPROVAL.getValue(),Arrays.asList("energySavingSealapproval","energySavingSealapprova200"));  //节能章审批

        variables.put(NoDrawingsApproveEnum.BUILDINGREGISTRAR.getValue(),Arrays.asList("energySavingSealapproval","energySavingSealapprova200"));  //建筑注册章
        variables.put(NoDrawingsApproveEnum.STRUCTUREREGISTRAR.getValue(),Arrays.asList("energySavingSealapproval","energySavingSealapprova200"));  //结构注册章
        variables.put(NoDrawingsApproveEnum.IMAGEFILEPERSONNEL.getValue(),Arrays.asList("imageFilePersonnel","imageFilePersonne2"));  //图档人员

//        variables.put(NoDrawingsApproveEnum.PROJECTOPERATIONMANAGEMENTDEPARTMENTDEPARTMENTHEAD.getValue(),Arrays.asList("projectOperationManagementDepartmentDepartmenthead1","projectOperationManagementDepartmentDepartmenthead2"));//项目运营管理部门负责人


        variables.put(NoDrawingsApproveEnum.PROJECTOPERATIONMANAGEMENTDEPARTMENTDEPARTMENTHEAD.getValue(),new ArrayList<>());
      //项目运营管理部
        variables.put(NoDrawingsApproveEnum.PROJECTOPERATIONMANAGEMENTDEPARTMENT.getValue(),Arrays.asList("projectOperationManagementDepartment1","projectOperationManagementDepartment2"));

        //质量办
    //    variables.put(NoDrawingsApproveEnum.TECHNICALQUALITYCENTEROFFFICE.getValue(),Arrays.asList("technicalQualityCenterOffice1","technicalQualityCenterOffice2"));

    //质量办
        variables.put(NoDrawingsApproveEnum.TECHNICALQUALITYCENTEROFFFICE.getValue(),new ArrayList<>());

       //盖章室
        variables.put(NoDrawingsApproveEnum.SEALCHAMBER.getValue(),Arrays.asList("sealChamber","sealChamber2"));
        //标识中国院审批还是标准院审批
        variables.put(NoDrawingsApproveEnum.COURTYARDTYPE.getValue(),NoDrawingsApproveEnum.CHINA.getValue());
        return variables;
    }

    /**
     * 流程部署
     */
    @Test
    public void testDeployment() {
//        1、创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        2、获取RepositoryServcie
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        3、使用service进行流程的部署，定义一个流程的名字，把bpmn和png部署到数据中
        Deployment deploy = repositoryService.createDeployment()
                .name("建筑工程文件用章申请")
                .key("第一次部署")
                //  技术文件用印洽商变更类
             //   .addClasspathResource("projectSeal/technologyRegardingTheChange.bpmn20.xml")
                //技术类文件 质量验收类
              //  .addClasspathResource("projectSeal/technologyQualityAcceptance.bpmn20.xml")
                // 技术文件用印往来联系类
              //  .addClasspathResource("projectSeal/technologyCorrespondenceClass.bpmn20.xml")
                //奖项申报类
             //  .addClasspathResource("projectSeal/technologyAwardApplicationCategory.bpmn20.xml")
               //技术文件其他技术文件
                .addClasspathResource("designDocument/designDocument202209080918.bpmn20.xml")
                // 经营及日常文件用印邀标文件查询函
               //  .addClasspathResource("projectSeal/businessInvitationToTenderDocumentsInquiryLetter.bpmn20.xml")

                // 经营及日常文件用印其它经营日常文件
            //    .addClasspathResource("projectSeal/businessAndRoutineDocumentsShallBePrintedOnOtherRoutineBusinessDocuments.bpmn20.xml")
                .deploy();

        System.out.println("流程部署id=" + deploy.getId());
        System.out.println("流程部署名字=" + deploy.getName());
        System.out.println("流程部署key=" + deploy.getKey());

    }


    /**
     * 启动流程实例
     * `act_hi_actinst`    流程实例执行历史信息
     * `act_hi_identitylink` 流程参与用户的历史信息
     * `act_hi_procinst`     流程实例的历史信息
     * `act_hi_taskinst`     流程任务的历史信息
     * `act_ru_execution`    流程执行信息
     * `act_ru_identitylink`  流程的正在参与用户信息
     * `act_ru_task`         流程当前任务信息
     */
    @Test
    public void testStartProcess() {
//        1、创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        2、获取RunTimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map varibales = getVaribales();
        // 3、根据流程定义的id启动流程 in_chapter_approval    results_confirm
       ProcessInstance instance = runtimeService.startProcessInstanceByKey("designDocument", varibales);
        System.out.println("流程定义ID：" + instance.getProcessDefinitionId());
        System.out.println("流程实例ID：" + instance.getId());
        System.out.println("当前活动的ID：" + instance.getActivityId());
    }





    /**
     * 完成任务
     */
    @Test
    public void testComplete() {
        String processId = "c2f2b217-2f14-11ed-a551-00ff251e0e80";
//        获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        TaskQuery taskQuery = taskService.createTaskQuery().processInstanceId(processId);
        List<Task> list = taskQuery.list();
        for (int i = 0; i < list.size(); i++) {
            Task task = list.get(i);
            try {

                taskService.complete(task.getId());
                System.out.println(task.getTaskDefinitionKey());
            }catch (Exception e){

            }
        }

    }
}
